<?php

class lunboControlPage extends commonControlPage {

    public function managerModule() {
        $act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
        call_user_func(array($this,$act.'Action'));
    }

    private function listAction() {
        $rowset = $_ENV['db']->fetchAll("SELECT * FROM lunbo ORDER BY lid DESC");
        $this->view->headlink = '<a href="?app=lunbo&mod=manager&act=add">添加</a>';
        $this->view->rowset = $rowset;
        $this->showMainPage('admincp/lunbo_list');
    }

    private function addAction() {
        if (POST) {
            $click_url = trim($_POST['click_url']);
            if (!$click_url or substr($click_url,0,7)!='http://') {
                exit("<script>alert('请确认连接地址');history.back()</script>");
            }
            $upload = new class_upload();
            $upload->setAllowExt('image');
            $filepath = "spread/".substr(md5(uniqid()),0,27);

            if (!$upload->upload('file',ATTACH_PATH.$filepath)) {
                exit("<script>alert('".$upload->getError()."');history.back()</script>");
            }

            $filepath = $filepath.'.'.$upload->getFileExt();

            $_ENV['db']->runSql("insert into lunbo ".$this->buildSql(array(
                    'pic' => $filepath,
                    'link_url' => $click_url
                )));
            exit("<script>alert('添加成功');window.location.href='?app=lunbo&mod=manager&act=list'</script>");
        }
        $this->showMainPage('admincp/lunbo_modi');
    }

    private function deleteAction() {
        $id = (int)$_GET['lid'];

        $l = $_ENV['db']->fetch("SELECT * FROM lunbo WHERE lid = '{$id}'");
        if ($l) {
            @unlink(ATTACH_PATH.$l['pic']);
            $_ENV['db']->runSql("DELETE FROM lunbo WHERE `lid` = '{$id}'");
        }
        exit("<script>alert('操作成功');window.location.href='?app=lunbo&mod=manager&act=list'</script>");
    }
}